<template>
  <base-layout
    :dialog-visible="dialogVisible"
    dialog-title="审核"
    @dialog-close="dialogClose"
    dialogWidth="600px"
  >
    <template #searchPanel>
      <el-row :gutter="20">
        <search-panel
          @search="search"
          :showAddButton="true"
          :showSearchButton="true"
          @insert="add"
          :addPer="authConfig.add"
          :selectPer="authConfig.query"
          addTitle="新增政策"
        >
          <el-input
            v-model="form.title"
            clearable
            placeholder="标题"
            style="width: 150px"
          />
          <el-date-picker
            v-model="timeList"
            type="datetimerange"
            range-separator="-"
            start-placeholder="申请开始日期"
            end-placeholder="申请结束日期"
            clearable
            value-format="yyyy-MM-dd hh:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
          <job-shop v-model="form.storeId" placeholder="适用门店" />
        </search-panel>
      </el-row>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @row-dblclick="detailHandle"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-search pointer"
            title="查看"
            v-permission="authConfig.detail"
            @click="detailHandle(row)"
          ></i>
          <i
            v-if="row.status === '待审批'"
            class="el-icon-edit"
            title="编辑"
            v-permission="authConfig.edit"
            @click="handleDataUpdateEdit(row, '编辑')"
          ></i>
          <i
            v-if="row.status === '待审批'"
            class="el-icon-delete"
            title="删除"
            v-permission="authConfig.del"
            @click="handleDataDelete(row)"
          ></i>
          <i
            v-if="row.status === '待审批'"
            class="el-icon-collection pointer"
            title="审批"
            v-permission="authConfig.sp"
            @click="approvalHandle(row)"
          ></i>
        </template>
      </data-table>
    </template>
    <template #dialog>
      <!-- <examine-approval
        @close="closeApprovalHandle"
        :value="approvalId"
      /> -->
      <approval-form
        :approval-data="approvalData"
        @save="approvalSave"
        code="code"
      />
    </template>
  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import { getDict } from '@/utils/index'
import { rulesForm } from './rules'
// import ExamineApproval from './examineApproval.vue' // 审批
import ApprovalForm from '@/components/CommonForm/ApprovalForm'

import {
  dataList,
  deleteData,
  auditIsOk,
} from '@/api/pre-sale/newCarStock/newCarSaleManagement'
export default {
  mixins: [
    baseMixin({
      idName: 'id',
      dataName: ['content', 'totalRecords'],
      findData: dataList,
      deleteData: deleteData,
    }),
  ],
  components: {
    ApprovalForm,
  },
  data() {
    return {
      authConfig: {
        query: 'jgzczd:page',
        add: 'jgzczd:save',
        edit: 'jgzczd:update',
        del: 'jgzczd:delete',
        detail: 'jgzczd:detail',
        sp: 'jgzczd:sp',
      },
      form: {
        title: '',
        sqStartTime: '',
        sqEndTime: '',
        storeId: '',
      },
      timeList: [],
      tableData: [],
      rule: rulesForm,
      approvalId: '', // 审批id
      tableColumnAttributes: [
        { name: '申请单号', value: 'code', type: 'text', width: '200' },
        { name: '适用店面', value: 'storeName', type: 'text', width: '200' },
        { name: '标题', value: 'title', type: 'text', width: '200' },
        { name: '备注', value: 'remark', type: 'text', width: '200' },
        { name: '申请日期', value: 'sqTime', type: 'text', width: '200' },
        { name: '申请人', value: 'sqrName', type: 'text', width: '200' },
        { name: '状态', value: 'status', type: 'text', width: '200' },
        { name: '处理人', value: 'clr', type: 'text', width: '200' },
      ],
      dialogVisible: false,
      approvalData: {},
    }
  },
  methods: {
    search() {
      if (this.timeList && this.timeList.length > 0) {
        this.form.sqStartTime = this.timeList[0]
        this.form.sqEndTime = this.timeList[1]
      } else {
        this.form.sqStartTime = ''
        this.form.sqEndTime = ''
      }
      this.searchOptions = this.form
      this.currentPage = 1
      this.setTableData()
    },
    //新增
    add() {
      this.$router.push({
        path: '/newCarStock/newCarSaleManagement/addpricePolicyMaking',
      })
    },
    handleDataUpdateEdit(row, type) {
      this.$router.push({
        path: '/newCarStock/newCarSaleManagement/editpricePolicyMaking',
        query: { id: row.id },
      })
    },
    // 查看
    detailHandle(row) {
      this.$router.push({
        path: '/newCarStock/newCarSaleManagement/viewpricePolicyMaking',
        query: { id: row.id, isView: true },
      })
    },
    // 审批
    approvalHandle(row) {
      this.approvalId = row.id
      this.approvalData = row
      this.dialogVisible = true
    },
    // closeApprovalHandle() {
    //   this.dialogVisible = false
    //   this.search()
    // },
    // 审核保存
    approvalSave(data) {
      auditIsOk({...data,id: this.approvalData.id}).then((res) => {
        const { success } = res
        if (success) {
          this.$message({
            message: '操作成功',
            type: 'success',
          })
          this.search()
        }
        this.dialogVisible = false
      })
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
